package recommenRatings.domain;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import recommenRatings.data.JDBCPhotoDAO;
import recommenRatings.data.JDBCUserDAO;

public class PhotoManager {
	private List<Photo> album;
	private Map<String, String> friends;
	private Set<String> amigos;
	private Set<String> friendsAsig;

	public PhotoManager(String userName) {
		JDBCUserDAO ju = new JDBCUserDAO();
		JDBCPhotoDAO jp = new JDBCPhotoDAO();
		album = jp.selectAllPhoto();
		friends = ju.selectAllFriends(userName);

	}
  
	public Set<User> findUser(Set<String> amigos){
		JDBCUserDAO jud = new JDBCUserDAO();
		Set<User> res = new HashSet<User>();
		for(String userPOID:amigos){
		  res.add(jud.select(userPOID));
		}
		return res;
	}
	
	public void PhotoAsig(String photo, Set<String> friendsAsig) {
		JDBCPhotoDAO jpd = new JDBCPhotoDAO();
		for (String s : friendsAsig) {
			jpd.insert(photo, s);
		}
	}

	public Set<String> getFriends( ) {
		Set<String> clave = friends.keySet();
		Set<String> amigos = new HashSet<String>();
		for (String s : clave) {
			amigos.add(friends.get(s));
		}
		return amigos;
	}

	public void addFriendsAsig(String u) {
		if (amigos.contains(u)) {
			if (friendsAsig == null) {
				friendsAsig = new HashSet<String>();
				friendsAsig.add(u);
			} else if (!friendsAsig.contains(u)) {
				friendsAsig.add(u);
			}

		}

	}

	public void removefriendsAsig(String u) {
		if (friendsAsig != null) {
			if (friendsAsig.contains(u)) {
				friendsAsig.remove(u);
			}
		}
	}

	public void removeAllFriendsAsig() {
		friendsAsig.clear();
	}

	public boolean addPhoto(Photo image) {
		boolean res = false;
		if (image.getPhoto() != null) {
			new JDBCPhotoDAO().insert(image);
			res = true;
		}
		return res;
	}

	public Photo showPhoto(String photoPOID) {
		Photo p = null;
		if (photoPOID != null) {
			JDBCPhotoDAO jp = new JDBCPhotoDAO();
			p = jp.select(photoPOID);
		}
		return p;
	}

	public Set<String> getAsigFriends(){
		return friendsAsig;
	}
	public List<Photo> showPhotoAll() {
		return album;
	}

}
